package ca.swank.hq.ai;

import ca.swank.hardquor.*;

/**
 * Creates an AIBenchmark for performance testing AI and board performance.
 * @author Jesse Wilson
 * @version 1.0 12-Aug-2002
 */
public class AIBenchmark implements hardquorUser
{
  private String serverIp;
  private String username;
  private hardquorClient client;
  private String userToChallenge;

  /**
   * constructs two new aiUsers which attempt to play a game of hardquor on the
   * specified server
   */
  public AIBenchmark(String serverIp, String username, String userToChallenge)
  { this.serverIp = serverIp;
    this.username = username;
    this.userToChallenge = userToChallenge;

    client = new hardquorClient(this);
    client.connect(serverIp);
  }

  /**
   * called upon connection to a hardquor game server. Upon having this method
   */
  public void notifyRequestUserName(String reason)
  { System.out.println("username requested for reason: " + reason);
    client.tryUsername(username);
  }

  /**
   * provides a list of online users upon connection to a hardquor game.
   *
   * the AIBenchmark iterates through each of these users, and calls the
   * notifyUserNew method on each one, so that if the challenged user exists,
   * then the game will start.
   */
  public void notifyUserList(String[] users)
  { System.out.println("received userlist: " + users);
    for(int i = 0; i < users.length; i++)
    { notifyUserNew(users[i]);
    }
  }

  /**
   * notification that another user has disconnected from the hardquor game
   */
  public void notifyUserGone(String username)
  { System.out.println("user gone: " + username);
  }

  /**
   * notification that another user has connected to the current hardquor game
   */
  public void notifyUserNew(String username)
  { System.out.println("user new: " + username);

    if(username.equals(userToChallenge))
    { client.makeGameRequest(new hardquorGameRequest(0, userToChallenge));
    }
  }

  /**
   * a chat message received from the specified user. Chat messages should
   */
  public void notifySpeak(String user, String message)
  { System.out.println(user + ": " + message);
  }

  /**
   * an incoming game request from another user on the current hardquor server.
   */
  public void notifyGameRequest(hardquorGameRequest request)
  { System.out.println("game requested");
    client.acceptGameRequest(request);
  }

  /**
   * requests the user create an instance of an implementation of the
   */
  public hardquorUserGame startGame(hardquorGameClient gameClient, int playerNumber)
  { System.out.println("game start");
    hardquorUserGame userGame = null;
    userGame = new TreeAIGame(gameClient, playerNumber, username);

    return userGame;
  }

  /**
   * starts a new WalkerUser which accepts all requests on specified server and name
   */
  public static void main(String[] args)
  { if(args.length != 3)
    { System.out.println("usage: AIBenchmark [server_ip] [userOne] [userTwo]");
    } else
    { AIBenchmark userOne = new AIBenchmark(args[0], args[1], null);
      AIBenchmark userTwo = new AIBenchmark(args[0], args[2], args[1]);
    }
  }

}